package com.truongnha.clienttn.presentation.fragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.truongnha.clienttn.R;
import com.truongnha.clienttn.business.model.TeacherAttendModel;
import com.truongnha.clienttn.business.service.TeacherAttendService;
import com.truongnha.clienttn.presentation.adapter.TeacherAttendAdapter;

import config.Url;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;

public class TeacherAttendanceFragment extends Fragment {

	private static String url_getAttendanceForStudent_allYear = Url.getHost()
			+ "api/getAttendanceForStudent/allYear";
	private Intent doneLoading;
	private List<TeacherAttendModel> list = new ArrayList<TeacherAttendModel>();
	private TeacherAttendAdapter teacherAttendAdapter;

	private ListView grid;
	private int day = 15;
	private int month = 4;
	private int year = 2013;
	private int classId = 86;
	String date;
	private Button btnDate;
	Calendar myCalendar = Calendar.getInstance();
	DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			myCalendar.set(Calendar.YEAR, year);
			myCalendar.set(Calendar.MONTH, monthOfYear);
			myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			updateDate();
			updateMainScreen();
		}
	};

	private String toStringDay(int dayOfWeek) {
		switch (dayOfWeek) {
		case 1:
			return "Chủ nhật";
		case 2:
			return "Thứ 2";
		case 3:
			return "Thứ 3";
		case 4:
			return "Thứ 4";
		case 5:
			return "Thứ 5";
		case 6:
			return "Thứ 6";
		case 7:
			return "Thứ 7";
		}
		return "";
	}

	private void updateMainScreen() {
		new TeacherAttendanceTask()
				.execute(url_getAttendanceForStudent_allYear);
	}

	private void updateDate() {
		this.year = myCalendar.get(Calendar.YEAR);
		this.month = myCalendar.get(Calendar.MONTH) + 1;
		this.day = myCalendar.get(Calendar.DAY_OF_MONTH);

		int dayOfWeek = myCalendar.get(Calendar.DAY_OF_WEEK);
		String currentDate = toStringDay(dayOfWeek) + ", " + day + "-" + month
				+ "-" + year;
		btnDate.setText(currentDate);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.teacher_attendance_template,
				container, false);
		btnDate = (Button) view.findViewById(R.id.date);

		btnDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new DatePickerDialog(getActivity(), d, myCalendar
						.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
						myCalendar.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
		updateDate();
		try {
			new TeacherAttendanceTask()
					.execute(url_getAttendanceForStudent_allYear);

			grid = (ListView) view.findViewById(R.id.teacher_attendance_grid);
			date = this.day + "-" + this.month + "-" + this.year;
			teacherAttendAdapter = new TeacherAttendAdapter(getActivity(),
					list, true, myCalendar);
			grid.setAdapter(teacherAttendAdapter);
		} catch (Exception e) {
			Log.e("log", "error in teacher attendance fragment " + e.toString());
		}
		return view;
	}

	class TeacherAttendanceTask extends AsyncTask<String, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			doneLoading = new Intent();
			doneLoading.setAction("DONE_LOADING_ACTION");
			doneLoading.putExtra("doneLoading", false);
			getActivity().getApplicationContext().sendBroadcast(doneLoading);
		}

		@Override
		protected Void doInBackground(String... params) {
			Log.d("log", " teacher attendance fragment 7");
			list.clear();
			TeacherAttendService teacherAttendService = new TeacherAttendService(
					getActivity().getApplicationContext());

			Log.d("log", " teacher attendance fragment 8");

			teacherAttendService.getDataFromServer(classId, day, month, year);
			Log.d("log", " teacher attendance fragment 9");

			teacherAttendService.getAttendFromDatabase(list);

			Log.d("log", " teacher attendance fragment 10");
			Log.d("log", "after get attend  size :" + list.size());
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			// Broadcast to main activity when done loading
			doneLoading = new Intent();
			doneLoading.setAction("DONE_LOADING_ACTION");
			doneLoading.putExtra("doneLoading", true);
			getActivity().getApplicationContext().sendBroadcast(doneLoading);
			teacherAttendAdapter.notifyDataSetChanged();
			// grid.smoothScrollToPosition(list.size());
		}
	}// End class getTermTask

}